let vm = new Vue({
  el: '#container',
  components: {
    'login': httpVueLoader('login.vue')//加载需要使用的vue文件
  },    
  data: {
    portal_url: VUE_APP_CLIENT_PORTAL_URL,
    coursePubId: 0,
    course_index: {},
    myCourseRecord: {},
    starRank: '',
    comment: {
      targetId: 0,
      starRank: 5,
      commentText: ''
    },
    commentStatistics: {},
    courseComments: [],
    courseCommentCounts: 0,
    hasEvaluated: false
  },
  computed: {
    courseProgress: function() {
      let url = '#'
      let text = ''

      // 课程发布id
      let coursePubId = this.coursePubId
      // 课程是否已付费
      let paid = this.myCourseRecord.paid
      // 课程学习进度
      let teachplanId = this.myCourseRecord.teachplanId
      // 是否是付费课程
      let charge = this.course_index.charge

      // 免费课程
      if (charge === '201000') {
        // 未学习
        if (!teachplanId) {
          url = `${this.portal_url}/learing-course-video.html?coursePubId=${coursePubId}`
          text = '马上学习'
        }
        // 学习过
        else {
          url = `${this.portal_url}/learing-course-video.html?coursePubId=${coursePubId}&teachplanId=${teachplanId}`
          text = '继续学习'
        }
      }
      // 付费课程
      else if (charge === '201001') {
        // 学习记录不存在，未付费
        if (paid !== 1) {
          url = `${this.portal_url}/learing-pay.html?coursePubId=${coursePubId}`
          text = '马上购买'
        }
        // 学习记录存在，已付费
        else {
          // 未学习
          if (!teachplanId) {
            url = `${this.portal_url}/learing-course-video.html?coursePubId=${coursePubId}`
            text = '马上学习'
          }
          // 学习过
          else {
            url = `${this.portal_url}/learing-course-video.html?coursePubId=${coursePubId}&teachplanId=${teachplanId}`
            text = '继续学习'
          }
        }
      }
      return {
        url: url,
        text: text
      }
    }
  },
  methods: {
    // 课程评论列表
    // TODO: 分页添加
    getCourseComment() {
      get_course_comment(
        1,
        10,
        JSON.stringify({
          targetId: this.coursePubId
        })
      ).then(data => {
        this.courseComments = data.items
        this.courseCommentCounts = data.counts
      })
    },
    // 提交课程评论
    handleSubmitCourseComment() {
      submit_course_comment(JSON.stringify(this.comment)).then(data => {
        this.hasEvaluated = true
        this.getCourseComment()
      })
    }
  },
  created() {
    let coursePubId = parseInt(localURL.searchParams.get('coursePubId'), 10)
    this.coursePubId = coursePubId
    this.comment.targetId = coursePubId

    // 获取课程发布信息，根据课程Id从索引库中查询数据
    search_course_index(this.coursePubId).then(data => {
      this.course_index = data
    })

    // 获取当前课程发布的学习进度
    my_course_record(this.coursePubId).then(data => {
      // 如果没有学习进度记录，后台会返回null
      if (data) {
        this.myCourseRecord = data
      }
    })

    // 课程评论统计(模拟)
    course_comment_statistics(this.coursePubId).then(data => {
      this.starRank = data.starRank
      this.commentStatistics = data
    })

    // 课程评论列表
    this.getCourseComment()
  },
  mounted() {
    $('.evaluate .star').mousemove(function(e) {
      var startX = $(this).offset().left
      var movX = e.clientX - startX + 0.5
      var w = 145 * (1 - movX / 145)
      $(this)
        .find('.score')
        .css('width', w + 'px')
      vm.comment.starRank = Math.ceil((movX / 145) * 5)
    })
  }
})
